package com.example.administrator.energyida.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.energyida.R;
import com.example.administrator.energyida.beans.Rank;

import java.util.ArrayList;
import java.util.List;

public class ItemListviewDashboardAdapter extends BaseAdapter {

    private List<Rank> objects = new ArrayList<Rank>();

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemListviewDashboardAdapter(Context context, List<Rank> objects) {
        this.objects = objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_listview_dashboard, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(position, (Object)getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(int pos, Object object, ViewHolder holder) {
        //TODO implement
        Rank rank = (Rank) object;
        if (pos % 2 == 0) {
            holder.gridItemDashTextId.setBackgroundResource(R.drawable.text_item_blue_bg);
            holder.gridItemDashTextName.setBackgroundResource(R.drawable.text_item_blue_bg);
            holder.gridItemDashTextEnergy.setBackgroundResource(R.drawable.text_item_blue_bg);
        }
        holder.gridItemDashTextId.setText(String.valueOf(rank.getId()));
        holder.gridItemDashTextName.setText(rank.getName());
        holder.gridItemDashTextEnergy.setText(rank.getPer());
    }

    protected class ViewHolder {
        private TextView gridItemDashTextId;
        private TextView gridItemDashTextName;
        private TextView gridItemDashTextEnergy;

        public ViewHolder(View view) {
            gridItemDashTextId = (TextView) view.findViewById(R.id.gridItem_dash_text_id);
            gridItemDashTextName = (TextView) view.findViewById(R.id.gridItem_dash_text_name);
            gridItemDashTextEnergy = (TextView) view.findViewById(R.id.gridItem_dash_text_energy);
        }
    }
}
